<?php

namespace App\Jobs;

use App\Models\Project;
use App\Models\Inspection;
use Illuminate\Bus\Queueable;
use App\Models\Inspection\Circle;
use App\Models\Inspection\Report;
use Illuminate\Support\Facades\DB;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\Statistical\StaffDimensionalityInspectionReports;
use App\Models\Statistical\ProjectDimensionalityInspectionReports;

/**
 * 巡检数据报表生成任务
 *
 * Date: 11/05/2018
 * @author George
 * @package App\Jobs
 */
class InspectionReportGenerate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

	protected $inspection;

	/**
	 * InspectionReportGenerate constructor.
	 * @param $inspection
	 */
	public function __construct(Inspection $inspection)
	{
		$this->inspection = $inspection;
	}

	/**
	 * 生成巡检统计报表
	 *
	 * Date: 12/05/2018
	 * @author George
	 */
    public function handle()
    {
		$this->projectDimensionality();
		$this->staffDimensionality();
    }

	/**
	 * 项目纬度报表生成逻辑
	 *
	 * Date: 12/05/2018
	 * @author George
	 */
	private function projectDimensionality()
	{
		$record = ProjectDimensionalityInspectionReports::query()->where([
			['company_id', '=', $this->inspection->company_id],
			['project_id', '=', $this->inspection->project_id],
			['date', '=', $this->inspection->created_at->toDateString()]
		])->first();

		if ($record) {
			$record->nodes_insepcted += 1;
			$record->nodes_rate = round($record->nodes_insepcted / $record->nodes_count) * 100;
			$record->nodes_errors += $this->inspection->status ? 0 : 1;
			$record->nodes_uninspected = $record->nodes_count - $record->nodes_insepcted;
			// 计算准点率：公式 已完成的点数-异常点数 / 总点数
			$record->nodes_punctuality = round(($record->nodes_insepcted - $record->nodes_errors) / $record->nodes_count) * 100;
		} else {
			/**
			 * 获取当前巡检记录对应的项目信息
			 */
			$project = Project::withCount('lines')->withCount('nodes')->withCount('inspections')->where('id', $this->inspection->project_id)->first();

			$attributes = [
				'company_id' => $this->inspection->company_id,
				'project_id' => $this->inspection->project_id,
				'project_name' => $project->name,
				'date' => $this->inspection->created_at->toDateString(),
				'lines_count' => $project->lines_count,
				'nodes_count' => $project->nodes_count,
				'nodes_insepcted' => $project->inspections_count,
				'nodes_rate' => round($project->inspections_count / $project->nodes_count) * 100,
				'nodes_punctuality' => $this->inspection->status ? (1 / $project->nodes_count) * 100 : 0,
				'nodes_uninspected' => $project->nodes_count - $project->inspections_count,
				'nodes_errors' => $this->inspection->status ? 0 : 1
			];
			ProjectDimensionalityInspectionReports::create($attributes);
		}
	}

	/**
	 * 人员纬度统计报表
	 *
	 * Date: 19/05/2018
	 * @author George
	 */
    private function staffDimensionality()
	{
		$record = StaffDimensionalityInspectionReports::query()->where([
			['company_id', '=', $this->inspection->company_id],
			['project_id', '=', $this->inspection->project_id],
			['schedule_id', '=', $this->inspection->schedule_id],
			['user_id', '=', $this->inspection->user_id],
		])->first();

		$circles_count = DB::table('circles')->where('line_id', $this->inspection->line_id)->count();
		/**
		 * 获取当前巡检记录对应的圈信息
		 */
		$circle = Circle::withCount('nodes')->withCount('inspections')->where('id', $this->inspection->circle_id)->first();

		if ($record) {
			// 判断圈的点数和完成的点数是否一致，如果一致则增加完成圈数
			$record->circles_inspected += $circle->nodes_count === $circle->inspections_count ? 1 : 0;
			$record->circles_uninspected = $circles_count - $record->circles_inspected;
			$record->circles_rate = round($record->circles_inspected / $circles_count) * 100;
			$record->nodes_inspected += 1;
			$record->nodes_uninspected -= 1;
			$record->nodes_rate = round($record->nodes_inspected / $record->nodes_count) * 100;

			// 判断巡检状态是否正常
			if (! $this->inspection->status) {
				$record->nodes_errors += 1;
			}
		} else {
			$attributes = [
				'company_id' => $this->inspection->company_id,
				'project_id' => $this->inspection->project_id,
				'user_id' => $this->inspection->user_id,
				'schedule_id' => $this->inspection->schedule_id,
				'line_id' => $this->inspection->line_id,
				'user_name' => $this->inspection->user_name,
				'circles_count' => $circles_count,
				'circles_inspected' => 0,
				'circles_uninspected' => $circles_count,
				'circles_rate' => 0,
				'nodes_count' => $circle->nodes_count,
				'nodes_inspected' => 1,
				'nodes_uninspected' => $circle->nodes_count - 1,
				'nodes_rate' => round(1 / $circle->nodes_count) * 100,
				'nodes_errors' => $this->inspection->status == 1 ? 0 : 1,
				'date' => $this->inspection->created_at->toDateString()
			];

			if ($this->inspection->status) {
				$attributes['nodes_punctuality'] = 100;
			} else {
				// 计算准点率
				$attributes['nodes_punctuality'] = 100 - (1 / $circle->nodes_count) * 100;
			}
			StaffDimensionalityInspectionReports::create($attributes);
		}
	}
}
